<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace Sun\Html;

class Script extends HtmlComponent {
    public $script;
    public function __construct($script = "", array $attr = array()) {
        parent::__construct("script");
        $this->script = $script;
        $this->SetAttribute("type", "text/javascript");
        $this->SetAttributes($attr);
    }
    public function HtmlContent()
    {
        return "/*<![CDATA[*/\n".$this->script."\n/*]]>*/";
    }
    public function JSONContent()
    {
        $cont = str_replace('\\', '\\\\', $this->script);
        $cont = str_replace('\'', '\\\'', $cont);
        $cont = str_replace('"', '\\"', $cont);
        $cont = str_replace("\n", "\\n ", $cont);
        return '[{ text: "'.$cont.'" }]';
    }
}